{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fc7b6957",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-02T07:04:47.771738Z",
     "start_time": "2024-11-02T07:04:47.417481Z"
    }
   },
   "outputs": [],
   "source": [
    "import geopandas as gpd\n",
    "from shapely.geometry import MultiPolygon, Polygon,LineString\n",
    "from shapely.geometry import Polygon, LineString, MultiLineString\n",
    "import numpy as np\n",
    "from shapely.ops import unary_union\n",
    "from py2d.Math import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5084b0b6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-02T07:04:48.676033Z",
     "start_time": "2024-11-02T07:04:47.774736Z"
    }
   },
   "outputs": [],
   "source": [
    "map_data_path = \"../map_data/hangzhou_natural.geojson\"\n",
    "\n",
    "gdf1 = gpd.read_file(map_data_path)\n",
    "\n",
    "gdf1 = gdf1[['osm_id', 'name', 'natural', 'geometry']]\n",
    "\n",
    "gdf1 = gdf1.sort_values(by='name', ascending=False).reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3c81a3da",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-02T07:04:48.691592Z",
     "start_time": "2024-11-02T07:04:48.677664Z"
    }
   },
   "outputs": [],
   "source": [
    "gdf_poly = gdf1['geometry']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2ec0bae2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-02T07:05:15.424439Z",
     "start_time": "2024-11-02T07:05:14.377193Z"
    }
   },
   "outputs": [],
   "source": [
    "result = []\n",
    "for poly in gdf_poly:\n",
    "    temp_poly = unary_union(poly).convex_hull\n",
    "    P = Polygon()\n",
    "    P = P.from_tuples(np.array(temp_poly.boundary.coords.xy).transpose())\n",
    "    depose = Polygon.convex_decompose(P)\n",
    "    \n",
    "    line_list= []\n",
    "    for i in depose:\n",
    "        line_list.append(LineString(np.array(i.as_tuple_list())))\n",
    "    diff_poly = temp_poly.difference(LineString(line_list[0]))\n",
    "\n",
    "    result.append(diff_poly)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f8b114f0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-02T07:05:16.354927Z",
     "start_time": "2024-11-02T07:05:16.345976Z"
    }
   },
   "outputs": [],
   "source": [
    "gdf1['convex_decompose'] = result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8b2e335f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-02T07:06:41.811460Z",
     "start_time": "2024-11-02T07:06:41.361968Z"
    }
   },
   "outputs": [],
   "source": [
    "gdf1.to_file(\"../map_data/convex_decompose.geojson\",driver='GeoJSON')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
